<?php

namespace App\Http\Controllers\Plugin;


use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Support\Facades\Storage;
class LocalUploadFileController extends Controller
{
    use ResponseData;
    public function upload(Request $request)
    {
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $photo = $request->file('file');
            $path = 'public/article/'.date('Ymd',time());
            Storage::makeDirectory($path);
            $file_name = uniqid();
            //存储本地
            $photo->storeAs($path,$file_name.'.'.$photo->getClientOriginalExtension());
            $img = 'storage/article/'.date('Ymd',time()).'/'.$file_name.'.'.$photo->getClientOriginalExtension();
            $domain = config('app.url');
            return $this->success($domain.$img);
        }else{
            return response('未上传任何文件');
        }
    }
    public function deleteFile(Request $request){
        $file = $request->input('file');
        $del = str_replace('storage','public',$file);
        $res = Storage::delete($del);
        return $this->success($res);

    }
    //文件下载功能
    public function download(Request $request){
        $file = $request->input('file');
        return response()->download(public_path($file));
    }
}
